package com.syni.mdd.yhd.common.all.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class GsonUtils {

	public static Gson gson = new GsonBuilder()
//			.addSerializationExclusionStrategy(new SpringPageExclusion())
//			.addSerializationExclusionStrategy(new HttpRequestExclusion())
//            .registerTypeAdapter(java.util.Date.class, new DateDeserializer()).setDateFormat(DateFormat.LONG)
			.create();

	public static <T> T fromJson(JsonObject json, Class<T> classOfT) {
		return gson.fromJson(json, classOfT);
	}

	public static <T> T fromJson(String json, Class<T> classOfT) {
		if (StringUtils.isBlank(json)) {
			return null;
		}
		return gson.fromJson(json, classOfT);
	}

	public static JsonObject parseToJsonObj(String jsonstr) {
		if (StringUtils.isBlank(jsonstr)) {
			return null;
		}
		return new JsonParser().parse(jsonstr).getAsJsonObject();
	}

	public static String getString(JsonObject jsonObj, String key) {
		if (jsonObj.has(key)) {
			return jsonObj.get(key).getAsString();
		}
		return null;
	}

	public static String parseToJson(Object obj) {
		return gson.toJson(obj);
	}

	public static JsonArray getArray(JsonObject jsonObj, String key) {
		if (jsonObj.has(key)) {
			return jsonObj.get(key).getAsJsonArray();
		}
		return null;
	}

}
